<?php
/*
 *      oscomponent.php
 *      
 *      Copyright 2011 ViRak <vetalrakitin@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

class oscomponent {
	
	protected $uuid;
	protected $active = true;
	private $hooks = array();
	protected $visible = true;
	protected $parent;
	public $height;
	public $width;
	public $top;
	public $left;
	
	function __construct($parent) {
		$this->uuid = genuuid();
		$this->parent = $parent;
		@session_start();
		$_SESSION["components"][$this->uuid] = $this;
		$this->oncreate();
		//$_SESSION["userinterface"]->addcomponent($this, $parent);
	}
	
	function destroy() {
		if ($this->ondestroy()) {
			foreach($this->getchildrens() as $childuuid) {
				$_SESSION["components"][$childuuid]->destroy();
			}
			$this->delete();
			$_SESSION["components"][$this->uuid] = null;
		}
	}	
	
	function getchildrens() {
		$ret = array();
		foreach($_SESSION["components"] as $component) {
			if ($component) {
				if ($component->parent === $this->uuid) {
					$ret[] = $component->uuid;//$component;
				}
			}
		}
		//$ret = $this->uuid;
		return $ret;
	}
	
	function show() {
		$this->onshow();
		$this->visible = true;
		$this->update();
	}
	
	function hide() {
		$this->onhide();
		$this->visible = false;
		$this->update();
	}
	
	function enable() {
		$this->onenable();
		$this->active = true;
		//$this->update();
	}
	
	function disable() {
		$this->ondisable();
		$this->active = false;
		//$this->update();
	}
	
	function __get($name) {
		switch ($name) {
			case "active":{return $this->active;break;}
			case "visible":{return $this->visible;break;}
			case "uuid":{return $this->uuid;break;}
			case "parent":{return $this->parent;break;}
		}
	}
	
	function setevent($object, $event, $func) {
		$this->hooks[$event][$func] = $object;
	}
	
	function setglobalevent($object, $event, $func) {
		$_SESSION["hooks"][$event][$func] = $object;
	}
	
	function __call($name, $arguments) {
		// in first search global hooks
		if (array_key_exists("hooks",$_SESSION)) {
			if (array_key_exists($name,$_SESSION["hooks"])) {
				foreach ($_SESSION["hooks"][$name] as $func=>$object) {
					if (array_key_exists($func,array_flip(get_class_methods(get_class($_SESSION["components"][$object]))))) {
						switch (count($arguments)) {
							case "0":{return $_SESSION["components"][$object]->{$func}($this->uuid);break;}
							case "1":{return $_SESSION["components"][$object]->{$func}($this->uuid,$arguments[0]);break;}
							case "2":{return $_SESSION["components"][$object]->{$func}($this->uuid,$arguments[0],$arguments[1]);break;}
						}
						
					}
				}
			}
		}
		// after search local hooks
		//if ($name == "oncellkeyup") {new osmsgbox("ghbt[fkb");}
		if (array_key_exists($name,$this->hooks)) {
			foreach ($this->hooks[$name] as $func=>$object) {
				if (array_key_exists($func,array_flip(get_class_methods(get_class($_SESSION["components"][$object]))))) {
					switch (count($arguments)) {
						case "0":{return $_SESSION["components"][$object]->{$func}();break;}
						case "1":{return $_SESSION["components"][$object]->{$func}($arguments[0]);break;}
						case "2":{return $_SESSION["components"][$object]->{$func}($arguments[0],$arguments[1]);break;}
					}
					
				}
			}
		}
		return true;
	}
	
/*
	function get($body) {
	}
*/
	
	function append() {
		$answer = &$_SESSION['answer'];
        $answer[] = array('append',$_SESSION["components"]["userinterface"]->uuid,$this->get());
	}
	
	function delete() {
		$answer = &$_SESSION['answer'];
        $answer[] = array('delete',$this->uuid);
	}
	
	function modify($param, $value) {
		$answer = &$_SESSION['answer'];
        $answer[] = array('modify',$this->uuid, $param, $value);
	}
	
	function setfocus() {
		$answer = &$_SESSION['answer'];
        $answer[] = array('setfocus',$this->uuid);
	}
	
	function update() {
		$answer = &$_SESSION['answer'];
        $answer[] = array('update',$this->uuid,$this->get());
	}
}
?>
